// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   change_ok.java

package logistics.product_providers;

import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataTag;

public class change_ok extends HttpServlet
{

	public change_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			HttpSession session = request.getSession();
			nseer_db_backup1 logistics_db = new nseer_db_backup1(dbApplication);
			if (logistics_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				counter count = new counter(dbApplication);
				ValidataTag vt = new ValidataTag();
				String product_ID = request.getParameter("product_ID");
				String product_name = exchange.unURL(request.getParameter("product_name"));
				String product_logistics_recommend_ID = request.getParameter("product_logistics_recommend_ID");
				String provider_IDn[] = request.getParameterValues("provider_ID");
				int m = 0;
				for (int j = 1; j < provider_IDn.length; j++)
				{
					String sql3 = (new StringBuilder("select * from logistics_product_providers_recommend_details where product_logistics_recommend_ID='")).append(product_logistics_recommend_ID).append("' and provider_ID='").append(provider_IDn[j]).append("'").toString();
					ResultSet rs3 = logistics_db.executeQuery(sql3);
					if (rs3.next())
					{
						String provider_ID_repeat = rs3.getString("provider_ID");
						m++;
					}
				}

				String provider_amount = request.getParameter("provider_amount");
				int num = Integer.parseInt(provider_amount);
				if (m != 0)
					response.sendRedirect((new StringBuilder("logistics/product_providers/change_ok_a.jsp?product_logistics_recommend_ID=")).append(product_logistics_recommend_ID).toString());
				else
				if (num == 0 && provider_IDn.length == 1)
				{
					response.sendRedirect((new StringBuilder("logistics/product_providers/change_ok_b.jsp?product_logistics_recommend_ID=")).append(product_logistics_recommend_ID).toString());
				} else
				{
					String recommender = request.getParameter("recommender");
					String bodyc = new String(request.getParameter("recommend_describe").getBytes("UTF-8"), "UTF-8");
					String recommend_describe = exchange.toHtml(bodyc);
					String change_time = request.getParameter("change_time");
					String changer = request.getParameter("changer");
					try
					{
						String sql = (new StringBuilder("update logistics_product_providers_recommend set change_time='")).append(change_time).append("',changer='").append(changer).append("',recommender='").append(recommender).append("',recommend_describe='").append(recommend_describe).append("',change_tag='1' where product_logistics_recommend_ID='").append(product_logistics_recommend_ID).append("'").toString();
						logistics_db.executeUpdate(sql);
						String provider_name = "";
						String provider_ID = "";
						for (int i = 1; i <= num; i++)
						{
							String tem_provider_name = (new StringBuilder("provider_name")).append(i).toString();
							String tem_provider_ID = (new StringBuilder("provider_ID")).append(i).toString();
							provider_name = request.getParameter(tem_provider_name);
							provider_ID = request.getParameter(tem_provider_ID);
							String sql1 = (new StringBuilder("update logistics_product_providers_recommend_details set details_number='")).append(i).append("' where product_logistics_recommend_ID='").append(product_logistics_recommend_ID).append("' and provider_ID='").append(provider_ID).append("'").toString();
							logistics_db.executeUpdate(sql1);
						}

						String provider_namen[] = request.getParameterValues("provider_name");
						for (int j = 1; j < provider_IDn.length; j++)
						{
							int details_number = num + j;
							String sql4 = (new StringBuilder("insert into logistics_product_providers_recommend_details(product_logistics_recommend_ID,details_number,provider_ID,provider_name) values ('")).append(product_logistics_recommend_ID).append("','").append(details_number).append("','").append(provider_IDn[j]).append("','").append(provider_namen[j]).append("')").toString();
							logistics_db.executeUpdate(sql4);
						}

						List rsList = GetWorkflow.getList(logistics_db, "logistics_config_workflow", "03");
						String elem[] = new String[3];
						if (rsList.size() == 0)
						{
							sql = (new StringBuilder("update logistics_product_providers_recommend set check_tag='1' where product_logistics_recommend_ID='")).append(product_logistics_recommend_ID).append("'").toString();
							logistics_db.executeUpdate(sql);
							String provider_group = "";
							for (int i = 1; i <= num; i++)
							{
								String sql1 = (new StringBuilder("select * from logistics_file where provider_ID='")).append(provider_ID).append("'").toString();
								ResultSet rs1 = logistics_db.executeQuery(sql1);
								if (rs1.next())
								{
									String recommend_products = rs1.getString("recommend_products");
									if (rs1.getString("recommend_products").indexOf(product_name) == -1)
										recommend_products = (new StringBuilder(String.valueOf(recommend_products))).append(product_name).append(", ").toString();
									String sql2 = (new StringBuilder("update logistics_file set recommend_products='")).append(recommend_products).append("' where provider_ID='").append(provider_ID).append("'").toString();
									logistics_db.executeUpdate(sql2);
								}
								provider_group = (new StringBuilder(String.valueOf(provider_group))).append(provider_name).append(", ").toString();
							}

							String sql3 = (new StringBuilder("select * from design_file where product_ID='")).append(product_ID).append("'").toString();
							ResultSet rs3 = logistics_db.executeQuery(sql3);
							if (rs3.next())
							{
								String sql4 = (new StringBuilder("update design_file set provider_group='")).append(provider_group).append("' where product_ID='").append(product_ID).append("'").toString();
								logistics_db.executeUpdate(sql4);
							}
						} else
						{
							sql = (new StringBuilder("delete from logistics_workflow where object_ID='")).append(product_logistics_recommend_ID).append("'").toString();
							logistics_db.executeUpdate(sql);
							sql = (new StringBuilder("update logistics_product_providers_recommend set check_tag='0' where product_logistics_recommend_ID='")).append(product_logistics_recommend_ID).append("'").toString();
							logistics_db.executeUpdate(sql);
							for (Iterator ite = rsList.iterator(); ite.hasNext(); logistics_db.executeUpdate(sql))
							{
								elem = (String[])ite.next();
								sql = (new StringBuilder("insert into logistics_workflow(config_id,object_ID,describe1,describe2) values ('")).append(elem[0]).append("','").append(product_logistics_recommend_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("')").toString();
							}

						}
					}
					catch (Exception ex)
					{
						ex.printStackTrace();
					}
					response.sendRedirect("logistics/product_providers/change_ok_c.jsp");
				}
				logistics_db.commit();
				logistics_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
